package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("session")
public class SessionController {
    @RequestMapping("create")
    public String createSession(HttpServletRequest req){
        //如果有存放该用户信息的session对象，则直接取出，否则，为该用户创建session对象
        HttpSession session = req.getSession();
        //在session中绑定共享对象，在一个会话范围内共享。
        session.setAttribute("userName","tom");


        return session.getId();
    }
    @RequestMapping("getInfo")
    public String getSessionInfo(HttpServletRequest req){
        HttpSession session = req.getSession();
        //从session中取出共享对象。
        Object obj = session.getAttribute("userName");
        return obj.toString();
    }
}
